package com.ainiyar.springweb.config.security.handler;

import com.ainiyar.springweb.common.result.ResultResponse;
import com.ainiyar.springweb.util.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**************************************
 * File Name : AnonymousAuthenticationHandler
 * Created with : IntelliJ IDEA.
 * Author : 艾尼亚尔·毛拉吾提
 * QQ : 524155792  WeChat : ainiyar0124
 * Created Time : 2024/2/4  17:47
 * Desc :匿名用户访问无权限资源时处理
 ***************************************/
@Component
public class AuthenticationHandler implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        //设置响应编码格式
        response.setContentType("application/json;charset=utf-8");
        ResultResponse<Object> resultResponse = ResultResponse.Fail("匿名用户无权限访问！", 400);
        WebUtil.renderString(response, resultResponse);
    }
}

